package org.hiucung.desktoptranslationtools.screens

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import cafe.adriel.voyager.core.screen.Screen
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import org.hiucung.desktoptranslationtools.dialog.openSaveFileDialog

/**
 * @title: HomeScreen
 * @projectName DesktopTranslationTools
 * @description: 导入页面
 * @author Kicc
 * @date 2025/7/22 2:27
 */
class ImportScreen : Screen {
    @Composable
    override fun Content() {
        Column(
            modifier = Modifier
                .padding(16.dp)
                .verticalScroll(rememberScrollState())
        ) {
            // 根目录选择部分
            SectionTitle("项目根目录选择")
            Text(
                text = "请在文件选择器中选择项目的根目录路径",
                style = MaterialTheme.typography.bodyMedium,
                modifier = Modifier.padding(bottom = 8.dp)
            )

            PathSelectionRow(
                label = "根目录路径:",
                path = "",
                buttonText = "选择目录",
                onButtonClick = {
                }
            )

            Spacer(modifier = Modifier.height(24.dp))

            // 导出路径选择部分
            SectionTitle("翻译文件导入设置")
            Text(
                text = "请选择翻译文件导入的路径和文件名",
                style = MaterialTheme.typography.bodyMedium,
                modifier = Modifier.padding(bottom = 8.dp)
            )

            PathSelectionRow(
                label = "导入路径:",
                path = "",
                buttonText = "选择文件",
                onButtonClick = {
                    openSaveFileDialog(
                        title = "请选择Excel表格文件保存路径",
                        description = "Excel表格文件",
                        selectFilePath = { filePath ->
                            filePath.takeIf { it.isNotEmpty() }?.run {
                            }
                        },
                        suffixNames = arrayOf("xlsx", "xls")
                    )
                }
            )

            Spacer(modifier = Modifier.height(32.dp))

            // 导出按钮
            Button(
                onClick = {

                },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(48.dp),
                shape = MaterialTheme.shapes.medium,
                colors = ButtonDefaults.buttonColors(
                    containerColor = MaterialTheme.colorScheme.primary
                )
            ) {
                Text(
                    text = "导入翻译文件",
                    style = MaterialTheme.typography.labelLarge
                )
            }
        }
    }

    @Composable
    private fun SectionTitle(text: String) {
        Text(
            text = text,
            style = MaterialTheme.typography.titleMedium,
            color = MaterialTheme.colorScheme.primary,
            modifier = Modifier.padding(bottom = 8.dp)
        )
    }

    @Composable
    private fun PathSelectionRow(
        label: String,
        path: String,
        buttonText: String,
        onButtonClick: () -> Unit
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(vertical = 8.dp)
        ) {
            Text(
                text = label,
                style = MaterialTheme.typography.bodyMedium,
                modifier = Modifier.weight(1f)
            )

            Spacer(modifier = Modifier.width(8.dp))

            Text(
                text = path.ifEmpty { "未选择" },
                style = MaterialTheme.typography.bodySmall,
                color = if (path.isEmpty()) MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
                else MaterialTheme.colorScheme.onSurface,
                modifier = Modifier.weight(2f)
            )

            Spacer(modifier = Modifier.width(16.dp))

            Button(
                onClick = onButtonClick,
                shape = MaterialTheme.shapes.small,
                colors = ButtonDefaults.outlinedButtonColors()
            ) {
                Text(buttonText)
            }
        }
    }
}